package com.panopset.sf.apps;

import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.List;

import com.panopset.centralen.resource.ImageResource;
import com.panopset.game.blackjack.BlackjackConfig;
import com.panopset.game.blackjack.ScreenSizer;
import com.panopset.game.card.Chip;
import com.panopset.game.card.ChipPainter;

public class SwingChipPainter {

    public SwingChipPainter(final BlackjackConfig config) {
        cp = new ChipPainter(config, chipImg.getWidth(null), chipImg.getHeight(null));
    }

    public int getChipHeight() {
        return cp.getChipHeight();
    }

    public int getChipWidth() {
        return cp.getChipWidth();
    }

    /**
     * Panopset owned image, created by Karl Dinwiddie 2012-05-19.
     */
    public static final String DFT_CHIP_IMAGE = "/com/panopset/game/card/chips.png";
    public final static Image chipImg = new ImageResource(DFT_CHIP_IMAGE)
            .getImage();

    public void paintChips(Graphics g, int x, int y, long amt) {
        ScreenSizer m = cp.c.getScreenSizer();
        List<Chip> stack = new ArrayList<Chip>();
        WHILE: while (amt > 99) {
            FOR: for (Chip chip : Chip.values()) {
                if (chip.getValue() == 250 && amt > 250) {
                    continue FOR;
                }
                if (amt >= chip.getValue()) {
                    stack.add(chip);
                    amt = amt - chip.getValue();
                    continue WHILE;
                }
            }
        }
        x = x + (2 * stack.size());
        for (Chip chip : stack) {
            paintChip(chip, g, x, y);
            y = y - (2 * m.getMultiplier());
            x = x - (1 * m.getMultiplier());
        }
    }

    private void paintChip(final Chip chip, final Graphics g, final int x,
            final int y) {
        int[] b = cp.getPaintDimensions(chip, x, y);
        g.drawImage(chipImg, b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
                null);
    }

    private final ChipPainter cp;
}
